import socket
import threading
import time

import server

'''import cmd2serial as cmd'''


class control:
    def __init__(self):
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.bind(('127.0.0.1', 8081))
        self.sock.listen(5)
        self.conn, self.client_add = self.sock.accept()
        '''self.parser = cmd.cmdparser()
        self.parser.openserialport()'''

    def msg_get(self):
        try:
            while True:
                msg = self.conn.recv(1024)
                data = msg.decode()
                data_list = eval(data)
                self.control(data_list)
                #print('接受到的列表为：')
                #print(data_list)
                if data == "":
                    break
        except:
            print('main.py 19 error')

    def control(self, data_list):
        if data_list[0] != 'em_stop':
            #self.parser.setspeed(data_list[1], data_list[2])
            print(data_list[1], data_list[2])
            time.sleep(0.05)
        else:
            #self.parser.stopcar(1)
            print(data_list[1], data_list[2])
            time.sleep(0.05)
        '''elif data_list == 'stop':
            #self.parser.stopcar(0)
            self.parser.setspeed(data_list[1], data_list[2])
            time.sleep(0.05)'''


def main():
    co = control()
    co.msg_get()


if __name__ == '__main__':
    try:
        mg = threading.Thread(target=main, args=())
        se = threading.Thread(target=server.main, args=())
        mg.start()
        se.start()
        mg.join()
        se.join()


    except:
        print('main out')
